package com.doupi.partner.op.controller.partner;

import com.doupi.partner.constant.PartnerTypeEnum;
import com.doupi.partner.manager.PartnerRelationManager;
import com.doupi.partner.model.PartnerRelation;
import com.doupi.partner.op.util.OpUtils;
import com.doupi.partner.service.PartnerRelationService;
import com.doupi.partner.service.SystemConfigService;
import com.doupi.partner.service.dto.AdministratorDto;
import com.doupi.partner.util.*;
import com.doupi.partner.util.constant.UtilConstants;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.junit.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

/**
 * 合伙人
 * <p>
 * TODO 权限控制
 *
 * @author Alvin Wang
 * @version 1.0.0 @ 2017-04-16
 */
@Controller
@RequestMapping("partner/{type}")
public class PartnerController {
	private final static String TEMPLATE_FOLDER = "partner/";

	private final PartnerRelationService partnerRelationService;

	private final PartnerRelationManager partnerRelationManager;

	private final SystemConfigService systemConfigService;

	@Autowired
	public PartnerController(PartnerRelationService partnerRelationService, PartnerRelationManager partnerRelationManager, SystemConfigService systemConfigService) {
		this.partnerRelationService = partnerRelationService;
		this.partnerRelationManager = partnerRelationManager;
		this.systemConfigService = systemConfigService;
	}

	@RequestMapping(value = "index", method = RequestMethod.GET)
	public String index(ModelMap modelMap, @PathVariable("type") Integer type) {
		modelMap.put("type", type);
		return TEMPLATE_FOLDER + "index";
	}

	@ResponseBody
	@RequestMapping(value = "list", method = RequestMethod.POST)
	public DataTableData<PartnerRelation> list(@PathVariable("type") Byte type, Integer offset, Integer limit, String startTime, String endTime, PartnerRelation partnerRelation) throws Exception {
		AdministratorDto administratorDto = OpUtils.getCurrentLoginAdministrator();
		PartnerRelation administratorRelation = administratorDto.getPartnerRelation();

		if (StringUtils.isNotEmpty(startTime)) {
			Date startDate = DateUtils.parseDate(startTime, UtilConstants.YYYY_MM_DD_HH_MM_SS);
			partnerRelation.setCreateTime(startDate);
		}
		if (StringUtils.isNotEmpty(endTime)) {
			Date endDate = DateUtils.parseDate(endTime, UtilConstants.YYYY_MM_DD_HH_MM_SS);
			partnerRelation.setUpdateTime(endDate);
		}
		PartnerTypeEnum partnerType = PartnerTypeEnum.getPartnerTypeByCode(administratorRelation.getType());
		Assert.assertNotNull(partnerType);
		switch (partnerType) {
		case HEADQUARTERS:// 总站
			break;
		case SUBSTATION:// 分站
			partnerRelation.setSubstationAdministratorId(administratorRelation.getAdministratorId());
			partnerRelation.setSuperiorAdministratorId(administratorRelation.getAdministratorId());
			break;
		case CITY: // 城市代理商
			partnerRelation.setCityAdministratorId(administratorRelation.getAdministratorId());
			partnerRelation.setSuperiorAdministratorId(administratorRelation.getAdministratorId());
			break;
		case SUPERIOR: // 一级合伙人
			partnerRelation.setSuperiorAdministratorId(administratorRelation.getAdministratorId());
			break;
		case SECOND:// 二级合伙人
			partnerRelation.setSuperiorAdministratorId(administratorRelation.getAdministratorId());
			// throw RuntimeBusinessException.createException("二级合伙人不能查看");
		default:
			throw RuntimeBusinessException.createException("未知的合伙人类型");
		}
		partnerRelation.setType(type);
		Pagination<PartnerRelation, PartnerRelation> pagination = new Pagination<>(offset, limit, partnerRelation);
		pagination = partnerRelationService.queryPartnerRelationsWithAnnotation(pagination);
		List<PartnerRelation> lists = partnerRelationManager.convertPartnerRelationListToDtoList(pagination.getRecords());
		return new DataTableData<>(pagination.getTotalCount(), lists);
	}

	// @ResponseBody
	// @RequestMapping(value = "delete/{id}", method = RequestMethod.DELETE)
	// public AjaxResponse delete(@PathVariable("id") Long id) {
	// partnerRelationService.deleteByPrimaryKey(id);
	// return AjaxResponse.defaultResponse();
	// }

	@RequestMapping(value = "edit/{id}", method = RequestMethod.GET)
	public String edit(ModelMap modelMap, @PathVariable("type") Byte type, @PathVariable(value = "id") Long id) {
		PartnerRelation partnerRelation = partnerRelationService.selectByPrimaryKey(id);
		if (null != partnerRelation) {
			modelMap.put("partnerRelation", partnerRelationManager.convertPartnerRelationToDto(partnerRelation));
		}
		modelMap.put("cities", systemConfigService.querySystemConfigsByParentKey("city"));
		modelMap.put("type", type);
		return TEMPLATE_FOLDER + "edit";
	}

	@RequestMapping(value = "save", method = RequestMethod.POST)
	public String save(String mobile, String name, PartnerRelation partnerRelation) {
		AdministratorDto administratorDto = OpUtils.getCurrentLoginAdministrator();
		partnerRelation.setNickname(name);
		PartnerTypeEnum partnerType = PartnerTypeEnum.getPartnerTypeByCode(partnerRelation.getType());
		AssertUtils.assertNotNull(partnerType, "异常操作");
		if (null == partnerRelation.getId()) { // 新增
			PartnerRelation administratorDtoPartnerRelation = administratorDto.getPartnerRelation();
			switch (partnerType) {
			case HEADQUARTERS: // 添加总站账号
				partnerRelation.setSuperiorAdministratorId(administratorDtoPartnerRelation.getAdministratorId());
				partnerRelation.setCityAdministratorId(administratorDtoPartnerRelation.getAdministratorId());
				partnerRelation.setSubstationAdministratorId(administratorDtoPartnerRelation.getAdministratorId());
				break;
			case SUBSTATION: // 添加分站账号
				partnerRelation.setSuperiorAdministratorId(administratorDtoPartnerRelation.getAdministratorId());
				partnerRelation.setCityAdministratorId(administratorDtoPartnerRelation.getCityAdministratorId());
				partnerRelation.setSubstationAdministratorId(administratorDtoPartnerRelation.getSubstationAdministratorId());
				break;
			case CITY: // 添加城市代理商
				partnerRelation.setSuperiorAdministratorId(administratorDtoPartnerRelation.getAdministratorId());
				partnerRelation.setCityAdministratorId(administratorDtoPartnerRelation.getCityAdministratorId());
				partnerRelation.setSubstationAdministratorId(administratorDtoPartnerRelation.getAdministratorId());
				break;
			case SUPERIOR: // 添加一级合伙人
				partnerRelation.setSuperiorAdministratorId(administratorDtoPartnerRelation.getAdministratorId());
				partnerRelation.setCityAdministratorId(administratorDtoPartnerRelation.getAdministratorId());
				partnerRelation.setSubstationAdministratorId(administratorDtoPartnerRelation.getSubstationAdministratorId());
				break;
			case SECOND: // 添加二级合伙人
				partnerRelation.setSuperiorAdministratorId(administratorDtoPartnerRelation.getAdministratorId());
				partnerRelation.setCityAdministratorId(administratorDtoPartnerRelation.getCityAdministratorId());
				partnerRelation.setSubstationAdministratorId(administratorDtoPartnerRelation.getSubstationAdministratorId());
				break;
			default:
				break;
			}

			if (StringUtils.isBlank(partnerRelation.getCity())) {
				partnerRelation.setCity(administratorDtoPartnerRelation.getCity());
			}
			// TODO 提取
			partnerRelation.setMarketingNumber("1" + RandomStringUtils.random(7, "1234567890"));
		}

		int ret = partnerRelationManager.savePartnerRelation(mobile, name, partnerRelation, "123456");
		if (ret <= 0) {
			throw RuntimeBusinessException.createException("编辑失败");
		}
		return "redirect:index";
	}
}
